package net.jlibrary.group.action;

import javax.annotation.Resource;

import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import net.jlibrary.common.core.annotation.Token;
import net.jlibrary.common.core.entity.Pagination;
import net.jlibrary.common.core.entity.PaginationParam;
import net.jlibrary.group.action.vo.MerchantAdminActionVo;
import net.jlibrary.group.service.IMerchantManager;

@RestController
@RequestMapping("/group")
public class MerchantAdminAction {
	@Resource
	private IMerchantManager merchantManager;

	@RequestMapping(value = "/merchantAdminView", method = { RequestMethod.GET })
	public ModelAndView view() {
		return new ModelAndView("/group/merchantAdminView");
	}

	@RequestMapping(value = "/merchantAdminJsonList", method = { RequestMethod.POST })
	public Pagination lists(MerchantAdminActionVo merchant, PaginationParam param) {
		Pagination pagination = merchantManager.listsPage(merchant, param);
		return pagination;
	}

	@RequestMapping(value = "/merchantAdminJsonDetail", method = { RequestMethod.GET })
	public MerchantAdminActionVo detail(MerchantAdminActionVo merchant) {
		merchant = merchantManager.get(merchant);
		return merchant;
	}

	@RequestMapping(value = "/merchantAdminJsonAddFormInit", method = { RequestMethod.GET })
	public MerchantAdminActionVo addFormInit() {
		MerchantAdminActionVo merchant = new MerchantAdminActionVo();
		return merchant;
	}

	@RequestMapping(value = "/merchantAdminJsonSave", method = { RequestMethod.POST })
	@Token
	public ModelMap save(@ModelAttribute("vm") MerchantAdminActionVo merchant, @Token String token,
			BindingResult result) {
		merchantManager.save(merchant);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/merchantAdminJsonUpdate", method = { RequestMethod.POST })
	@Token
	public ModelMap update(@ModelAttribute("vm") MerchantAdminActionVo merchant, @Token String token,
			BindingResult result) {
		merchantManager.update(merchant);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/merchantAdminJsonDelete", method = { RequestMethod.POST })
	public ModelMap delete(Long id) {
		MerchantAdminActionVo merchant = new MerchantAdminActionVo();
		merchant.setId(id);
		merchantManager.delete(merchant);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/merchantAdminJsonRenew", method = { RequestMethod.POST })
	public ModelMap renew(Long id) {
		MerchantAdminActionVo merchant = new MerchantAdminActionVo();
		merchant.setId(id);
		merchantManager.renew(merchant);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

}
